/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2015
 */


package com.environment_pro.monitor.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.environment_pro.monitor.model.TbDevicePara;
import com.environment_pro.monitor.service.TbDeviceParaService;
import com.environment_pro.monitor.util.StaticUtil;
import com.environment_pro.monitor.util.PageHelper;
import com.environment_pro.monitor.util.PageHelper.Page;

@Controller
@RequestMapping("/tbDevicePara")
public class TbDeviceParaController extends BaseController {
	
	private static final Logger logger = Logger.getLogger(TbDeviceParaController.class);

	@Autowired
	private TbDeviceParaService tbDeviceParaService;
	
	/**
	 * 获取所有列表 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllTbDevicePara")
	public String getAllTbDevicePara(TbDevicePara tbDevicePara, HttpServletRequest request){
		//初始化分页 参数
		initPageParameter(request);
		//查询
		Page<TbDevicePara> mypage = tbDeviceParaService.findAll(pageInfo,tbDevicePara);
		//返回查询条件和结果
		request.setAttribute("mypage", mypage);
		request.setAttribute("tbDevicePara", tbDevicePara);
		request.setAttribute("tbDeviceParaList", mypage.getResult());
		//返回页面
		return "/TbDevicePara/allTbDevicePara";
	}
	
	/**
	 * 获取所有列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllTbDeviceParaJson")
	@ResponseBody
	public List<TbDevicePara> getAllTbDeviceParaJson(TbDevicePara tbDevicePara,HttpServletRequest request){
		//初始化分页 参数
		initPageParameter(request);
		Page<TbDevicePara> mypage = tbDeviceParaService.findAll(pageInfo,tbDevicePara);
		
		return mypage.getResult();
	}
	
	/**
	 * 跳转到添加界面
	 * @param request
	 * @return
	 */
	@RequestMapping("/toAddTbDevicePara")
	public String toAddTbDevicePara(HttpServletRequest request){
		
		return "/TbDevicePara/addTbDevicePara";
	}
	/**
	 * 添加并重定向
	 * @param tbDevicePara
	 * @param request
	 * @return
	 */
	@RequestMapping("/addTbDevicePara")
	public String addTbDevicePara(TbDevicePara tbDevicePara,HttpServletRequest request){
		tbDeviceParaService.save(tbDevicePara);
		this.saveLogOperate(request,"");
		logger.info("");
		return "redirect:/tbDevicePara/getAllTbDevicePara";
	}
	
	/**
	 *编辑
	 * @param tbDevicePara
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateTbDevicePara")
	public String updateTbDevicePara(TbDevicePara tbDevicePara,HttpServletRequest request){
		
		if(tbDeviceParaService.update(tbDevicePara)){
			//tbDevicePara = tbDeviceParaService.findById(tbDevicePara.getId());
			request.setAttribute("tbDevicePara", tbDevicePara);
			this.saveLogOperate(request,"");
			return "redirect:/tbDevicePara/getAllTbDevicePara";
		}else{
			return "/error";
		}
	}
	/**
	 * 根据id查询单个
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getTbDevicePara")
	public String getTbDevicePara(int id,HttpServletRequest request){
		
		request.setAttribute("tbDevicePara", tbDeviceParaService.findById(id));
		return "/TbDevicePara/editTbDevicePara";
	}
	
	/**
	 * 根据id查询单个
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/getTbDeviceParaJson")
	@ResponseBody
	public TbDevicePara getTbDeviceParaJson(int id,HttpServletRequest request){
		return tbDeviceParaService.findById(id);
	}
	
	/**
	 * 删除
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delTbDevicePara")
	public void delTbDevicePara(int id,HttpServletRequest request,HttpServletResponse response){
		String result = "{\"result\":\"error\"}";
		TbDevicePara tbDevicePara = tbDeviceParaService.findById(id);
		if(tbDeviceParaService.delete(id)){
			result = "{\"result\":\"success\"}";
		}
		this.saveLogOperate(request,tbDevicePara.toString());
		response.setContentType("application/json");
		this.saveLogOperate(request,"");
		try {
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

